﻿Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text


'AES IMPLEMENTATION

Public Class CryptographyRC4
    'working

    'Initialization Vector(IV) Used for Encrypt and Decrypt Values
    Private mRC2IV As Byte() = {1, 2, 3, 4, 5, 6, 7, 8}

    Function EncryptRC4(ByVal Key As String, ByVal Text As String) As String
        Dim ValueByte As Byte()
        Dim KeyByte As Byte()
        Dim objCrypto As New System.Security.Cryptography.RC2CryptoServiceProvider
        Dim bytBuffer As Byte()
        Dim streamClearText As System.Security.Cryptography.CryptoStream
        Dim streamCypherText As System.IO.MemoryStream
        Dim bytResult As Byte()

        'Convert String to Bytes using UTF8
        ValueByte = System.Text.Encoding.UTF8.GetBytes(Text)

        KeyByte = System.Text.ASCIIEncoding.ASCII.GetBytes(Left(Key, 16))

        bytBuffer = ValueByte
        streamCypherText = New System.IO.MemoryStream
        streamClearText = New System.Security.Cryptography.CryptoStream(streamCypherText, objCrypto.CreateEncryptor(KeyByte, mRC2IV), System.Security.Cryptography.CryptoStreamMode.Write)
        streamClearText.Write(bytBuffer, 0, bytBuffer.Length)
        streamClearText.FlushFinalBlock()
        bytResult = streamCypherText.ToArray
        'CONVERT BYTE() BACK TO STRING USING TOBASE64 AS WE MIGHT NEED THE VALUE TO WRITE TO FILE OR PASS AS URL
        Return Convert.ToBase64String(bytResult)
    End Function


    Function DecryptRC4(ByVal Key As String, ByVal Text As String) As String
        Dim ValueByte As Byte()
        Dim KeyByte As Byte()
        Dim objCrypto As New System.Security.Cryptography.RC2CryptoServiceProvider
        Dim bytBuffer As Byte()
        Dim streamCypherText As System.Security.Cryptography.CryptoStream
        Dim streamClearText As System.IO.MemoryStream
        Dim bytResult As Byte()

        'USE FROMBASE64STRING AS ENCRYPT FUNCTION CONVERTS THE STRING TO TOBASE64STRING
        ValueByte = Convert.FromBase64String(Text)
        KeyByte = System.Text.ASCIIEncoding.ASCII.GetBytes(Left(Key, 16))

        bytBuffer = ValueByte
        streamClearText = New System.IO.MemoryStream
        streamCypherText = New System.Security.Cryptography.CryptoStream(streamClearText, objCrypto.CreateDecryptor(KeyByte, mRC2IV), System.Security.Cryptography.CryptoStreamMode.Write)
        streamCypherText.Write(bytBuffer, 0, bytBuffer.Length)

        streamCypherText.FlushFinalBlock()
        bytResult = streamClearText.ToArray
        'CONVERT BYTE() BACK TO STRING USING UTF8
        Return System.Text.Encoding.UTF8.GetString(bytResult)
    End Function
End Class

Public Class CryptographyRSA
    'incomplete
    Function DecryptRSA(ByVal Key, ByVal Text) As String

        'Create a UnicodeEncoder to convert between byte array and string.
        Dim ByteConverter As New System.Text.UTF8Encoding

        'Create byte arrays to hold original, encrypted, and decrypted data.
        Dim dataToDecrypt As Byte() = ByteConverter.GetBytes(Text)
        Dim decryptedData() As Byte

        'Create a new instance of RSACryptoServiceProvider to generate
        'public and private key data.
        Dim RSA As New System.Security.Cryptography.RSACryptoServiceProvider

        Dim Parameters As New System.Security.Cryptography.RSAParameters
        Parameters.P = ByteConverter.GetBytes(Key, 0, 10)
        Parameters.Q = ByteConverter.GetBytes(Key, 10, 10)


        'Pass the data to DECRYPT, the private key information 
        '(using RSACryptoServiceProvider.ExportParameters(true),
        'and a boolean flag specifying no OAEP padding.
        decryptedData = RSA.Decrypt(dataToDecrypt, False)

        'Return System.Text.Encoding.UTF8.GetString(decryptedData)
        Return decryptedData.ToString

    End Function

    Function EncryptRSA(ByVal Key, ByVal Text) As String

        'Create a UnicodeEncoder to convert between byte array and string.
        Dim ByteConverter As New System.Text.UnicodeEncoding()

        'Create byte arrays to hold original, encrypted, and decrypted data.
        Dim dataToEncrypt As Byte() = ByteConverter.GetBytes(Text)
        Dim encryptedData() As Byte



        'Create a new instance of RSACryptoServiceProvider to generate
        'public and private key data.
        Dim RSA As New System.Security.Cryptography.RSACryptoServiceProvider


        Call MsgBox(RSA.ToXmlString(True))

        Call MsgBox("Keysize = " & RSA.KeySize)

        'Pass the data to ENCRYPT, the public key information 
        '(using RSACryptoServiceProvider.ExportParameters(false),
        'and a boolean flag specifying no OAEP padding.
        encryptedData = RSA.Encrypt(dataToEncrypt, False)

        Return encryptedData.ToString

    End Function

End Class

Public Class CryptographyAES

    Public EncodingFormat As System.Text.Encoding = System.Text.Encoding.ASCII
    Public IV() As Byte = System.Text.Encoding.UTF8.GetBytes(Left("sdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jjsdfkljdsfoiu089823o4j23oijesfFKSDJ432oi2j34oij23lk4j3l2j432li4jj", 16))
    Public KeyLengthBits As Integer = 256

    Public Function LeftByte(ByVal ByteIn As Byte(), ByVal Length As Integer) As Byte()

        Dim ByteOut As Byte() = {}
        ReDim ByteOut(Length - 1)
        Dim count As Integer
        For count = 0 To Length - 1
            ByteOut(count) = ByteIn(count)
        Next

        Return ByteOut

    End Function

    Function Encrypt(ByVal KeyIn As String, ByVal PlainText As String) As String


        Dim Key As Byte() = LeftByte(EncodingFormat.GetBytes(KeyIn), 32)

        ' Check arguments.
      
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If

        ' Declare the streams used
        ' to encrypt to an in memory
        ' array of bytes.
        Dim msEncrypt As MemoryStream = Nothing
        Dim csEncrypt As CryptoStream = Nothing
        Dim swEncrypt As StreamWriter = Nothing

        ' Declare the AesManaged object
        ' used to encrypt the data.
        Dim aesAlg As AesManaged = Nothing



        ' Declare the bytes used to hold the
        ' encrypted data.
        ' Dim encrypted As Byte() = Nothing


        Try
            ' Create a AesManaged object
            ' with the specified key and IV.
            aesAlg = New AesManaged()


            aesAlg.Key = Key
            aesAlg.IV = IV

            ' Create a decrytor to perform the stream transform.
            Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

            ' Create the streams used for encryption.
            msEncrypt = New MemoryStream()
            csEncrypt = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
            swEncrypt = New StreamWriter(csEncrypt)

            'Write all data to the stream.
            swEncrypt.Write(PlainText)


        Finally
            ' Clean things up.
            ' Close the streams.
            If Not (swEncrypt Is Nothing) Then
                swEncrypt.Close()
            End If
            If Not (csEncrypt Is Nothing) Then
                csEncrypt.Close()
            End If
            If Not (msEncrypt Is Nothing) Then
                msEncrypt.Close()
            End If
            ' Clear the AesManaged object.
            If Not (aesAlg Is Nothing) Then
                aesAlg.Clear()
            End If
        End Try




        Return Convert.ToBase64String(msEncrypt.ToArray())

    End Function


    Function Decrypt(ByVal KeyIn As String, ByVal cipherTextIn As String) As String

        Dim Key As Byte() = LeftByte(EncodingFormat.GetBytes(KeyIn), 32)
        Dim cipherText As Byte() = Convert.FromBase64String(cipherTextIn)

        ' Check arguments.
        If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
            Throw New ArgumentNullException("cipherText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        ' TDeclare the streams used
        ' to decrypt to an in memory
        ' array of bytes.
        Dim msDecrypt As MemoryStream = Nothing
        Dim csDecrypt As CryptoStream = Nothing
        Dim srDecrypt As StreamReader = Nothing

        ' Declare the AesManaged object
        ' used to decrypt the data.
        Dim aesAlg As AesManaged = Nothing

        ' Declare the string used to hold
        ' the decrypted text.
        Dim plaintext As String = Nothing

        Try
            ' Create a AesManaged object
            ' with the specified key and IV.
            aesAlg = New AesManaged()
            aesAlg.Key = Key
            aesAlg.IV = IV

            ' Create a decrytor to perform the stream transform.
            Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

            ' Create the streams used for decryption.
            msDecrypt = New MemoryStream(cipherText)
            csDecrypt = New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
            srDecrypt = New StreamReader(csDecrypt)

            ' Read the decrypted bytes from the decrypting stream
            ' and place them in a string.
            plaintext = srDecrypt.ReadToEnd()
        Finally
            ' Clean things up.
            ' Close the streams.
            If Not (srDecrypt Is Nothing) Then
                srDecrypt.Close()
            End If
            If Not (csDecrypt Is Nothing) Then
                csDecrypt.Close()
            End If
            If Not (msDecrypt Is Nothing) Then
                msDecrypt.Close()
            End If
            ' Clear the AesManaged object.
            If Not (aesAlg Is Nothing) Then
                aesAlg.Clear()
            End If
        End Try

        Return plaintext

    End Function


End Class
